package com.pf.java.File;

import java.io.*;

/**
 * 序列化版本
 *
 * ClassName:IO12
 * Package:com.pf.java.File
 *
 * @author: pf
 * @date: 2021/11/8-16:12
 */
public class IO12 {

    public static void main(String[] args) {
//        testSerialize();
        testDeserialize();
    }


    /**
     * 序列化
     */
    public static void testSerialize() {
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/dri.txt")
                );
        ){
            Driver d = new Driver("Tom", 'M', 23);
//            d.setLicense("111");
//            d.setLevel(2);
            oos.writeObject(d);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 反序列化
     */
    public static void testDeserialize() {
        try (
                ObjectInputStream ois = new ObjectInputStream(
                        new FileInputStream("C:/Users/10970/Desktop/Java学习笔记/牛客网学习/dri.txt")
                );
        ){
            Driver d = (Driver) ois.readObject();
            System.out.println(d);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

class Driver implements Serializable {

    private static final long serialVersionUID = -1007117913933111287L;
    private String name;
    private char gender;
    private int age;
    private String licenseNO;// 序列化后的改动
//    private int level;
    private int order;




    public Driver(String name, char gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getLicenseNO() {
        return licenseNO;
    }

    public void setLicenseNO(String licenseNO) {
        this.licenseNO = licenseNO;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "Driver{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", licenseNO='" + licenseNO + '\'' +
                ", order=" + order +
                '}';
    }
}